package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.AllProperty;
import domain.PropertyExc;
import repository.AllPropertyDAO;
import repository.PropertyExcDAO;

@WebServlet("/PropExcInc")
public class PropExcInc extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
    public PropExcInc() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		PropertyExc propex = new PropertyExc();
		
		propex.setID(request.getParameter("ID"));
		propex.setName(request.getParameter("name"));
		propex.setSouLocation(request.getParameter("sorlocation"));
		propex.setExcLocation(request.getParameter("exlocation"));
		propex.setNumber(request.getParameter("number"));
		
		PropertyExcDAO proexDao =null;
		try {
			proexDao = new PropertyExcDAO();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		proexDao.insert(propex);
		
		AllPropertyDAO alldao = null;
		try {
			alldao = new AllPropertyDAO();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ResultSet rs = alldao.query(request.getParameter("ID"));
		AllProperty all = new AllProperty();
		try {
			while(rs.next()) {
				all.setID(rs.getString("物品ID"));
				all.setName(rs.getString("物品名称"));
				all.setPrice(rs.getString("物品价格"));
				all.setLocation(rs.getString("所属学院"));
				all.setUse(rs.getString("使用状况"));
				all.setNumber(rs.getString("数量"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		AllProperty allp = new AllProperty();
		allp.setID(all.getID());
		allp.setName(all.getName());
		allp.setPrice(all.getPrice());
		allp.setLocation(request.getParameter("exlocation"));
		allp.setUse(all.getUse());
		allp.setNumber(all.getNumber());
		alldao.update(allp);
		request.getRequestDispatcher("PropertyExcDate.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
